package Triangulation.minimalityMetrics;

import datatypes.Edge;
import datatypes.IndexedFaceSet;
import datatypes.Triangle;
import datatypes.list.LinkedList;

public abstract class AbstractMinimalityMetric implements MinimalityMetric {

	@Override
	public boolean localMinimumExistsForEdge(Edge edge, IndexedFaceSet set) {
		
		// Get all the empty triangles adjacent to this particular edge.
		LinkedList<Triangle> trianglesLeft = edge.getTrianglesLeft();
		LinkedList<Triangle> trianglesRight = edge.getTrianglesRight();
		
		while (trianglesLeft.hasNext()) {
			Triangle triLeft = trianglesLeft.next();
			while (trianglesRight.hasNext()) {
				Triangle triRight = trianglesRight.next();
				if (edgeIsLocallyMinimal(edge, triLeft, triRight)) {
					// If we find ANY local minimum for the edge e, it means that the edge
					// e remains a candidate, and cannot be removed
					
					// moving the index of the trianglesList to the previous position to 
					// make sure the next time the exact same set of triangles is
					// tested first again. The same set will most likely return true
					// again causing a significant speedup!
					trianglesLeft.previous();
					trianglesRight.previous();
					return true;
				}
			}
			trianglesRight.moveToHead();
		}
		return false;
	}
	
//	@Override
//	public boolean localMinimumExistsForEdge(Edge edge, IndexedFaceSet set) {
//		boolean foundLocalMinimum = false;
//		
//		// Get all the empty triangles adjacent to this particular edge.
//		DoublyLinkedList<Triangle> trianglesLeft = edge.getTrianglesLeft();
//		DoublyLinkedList<Triangle> trianglesRight = edge.getTrianglesRight();
//		trianglesLeft.moveToHead();
//		trianglesRight.moveToHead();
//		
//		checkingEdgeForLocalMinimality:
//		while (trianglesLeft.hasNext()) {
//			Triangle triLeft = trianglesLeft.next();
//			while (trianglesRight.hasNext()) {
//				Triangle triRight = trianglesRight.next();
//				if (edgeIsLocallyMinimal(edge, triLeft, triRight)) {
//					// If we find ANY local minimum for the edge e, it means that the edge
//					// e remains a candidate, and cannot be removed
//					foundLocalMinimum = true;
//					break checkingEdgeForLocalMinimality; // jumps out of both loops.
//				}
//			}
//			trianglesRight.moveToHead();
//		}
//		
//		return foundLocalMinimum;
//	}

}
